package com.education.welco.controller;


import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.welco.pojo.*;
import com.education.welco.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 课程作业 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@RequestMapping("/sCatalogWork")
public class SCatalogWorkController {

    @Autowired
    private ISCatalogWorkService catalogWorkService;
    @Autowired
    private ISCatalogWorkSynService catalogWorkSynService;
    @Autowired
    private ITClassService classService;
    @Autowired
    private ITSchooldeptService schooldeptService;
    @Autowired
    private ISHomewordClassService homewordClassService;
    @Autowired
    private ISCatalogService catalogService;
    @Autowired
    private ISCatalogMenuService catalogMenuService;
    @Autowired
    private IStudentWorkService studentWorkService;


    /**
     * 查看班级
     */
    @GetMapping("/answerClass")
    public ApiReturnObject answerClass(Integer catalogId) {
        if (catalogId != null) {
            List<TClass> classList = classService.selectTClassByCatalogIdWorkAll(catalogId);
            if (classList != null && classList.size() > 0) {
                List<TClass> dept = findDept(classList);
                return new ApiReturnObject("200", "查询成功", dept);
            }
            return new ApiReturnObject("200", "结果为空", classList);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 修改查看班级
     */
    @PostMapping("/updateClass")
    public ApiReturnObject updateClass(@RequestBody SCatalogWork catalogWork) {
        //workId             classList
        if (catalogWork.getId() != null) {
            SHomewordClass homewordClass = new SHomewordClass();
            homewordClass.setWorkId(catalogWork.getId());
            //先删除中间表数据
            homewordClassService.deleteSHomewordClassByCatalogId(catalogWork.getId());
            if (catalogWork.getClassList() != null && catalogWork.getClassList().size() > 0) {
                homewordClass.setCreateTime(new Date());
                for (TClass tClass : catalogWork.getClassList()) {
                    homewordClass.setClassId(tClass.getId());
                    homewordClassService.insertSHomewordClass(homewordClass);
                }
            }
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 查看内容 针对于左侧目录  1
     */
    @GetMapping("/ansContent")
    public ApiReturnObject ansContent(Integer catalogId) {
        //查出该目录下的所有课程问答     将选中的属性设置为true
        if (catalogId != null) {
            //全部内容集合
            SCatalogWork catalogWork = new SCatalogWork();
            catalogWork.setCatalogId(catalogId);
            List<SCatalogWork> sCatalogWorks = catalogWorkService.selectSCatalogWorkList(catalogWork);
            //选中内容集合
            List<SCatalogWork> catalogWork1 = catalogWorkService.selectSCatalogWorkByCatalogId(catalogId);
            if (catalogWork1 != null && catalogWork1.size() > 0) {
                for (SCatalogWork sCatalogWork : catalogWork1) {
                    for (SCatalogWork work : sCatalogWorks) {
                        if (sCatalogWork.getId().equals(work.getId())) {
                            work.setChecked(true);
                        }
                    }
                }
            }
            return new ApiReturnObject("200", "查询成功", sCatalogWorks);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 修改查看内容
     */
    @PostMapping("/updateContent")
    public ApiReturnObject updateContent(@RequestBody SCatalogWork catalogWork) {
        //catalogId     type        classList
        if (catalogWork.getCatalogId() != null) {
            SCatalogWorkSyn workSyn = new SCatalogWorkSyn();
            workSyn.setCatalogId(catalogWork.getCatalogId());
            List<SCatalogWorkSyn> sCatalogWorkSyns = catalogWorkSynService.selectSCatalogWorkSynList(workSyn);
            //中间表中存在数据
            if (sCatalogWorkSyns != null && sCatalogWorkSyns.size() > 0) {
                // 改变往中间添加的数据的状态
                SCatalogWork catalogWork1 = catalogWorkService.selectSCatalogWorkById(sCatalogWorkSyns.get(0).getWorkId());
                //根据中间表id 获取数据
                if (catalogWork1 != null) {
                    //选中内容的id集合     传过来的是id集合
                    if (catalogWork.getAnsweringList() != null && catalogWork.getAnsweringList().size() > 0) {
                        //新建一个集合
                        ArrayList<Integer> list = new ArrayList<>();
//                        List<SAnswering> answeringList = answeringService.selectSAnsweringByIds(answering.getAnswerIdList());
                        for (SCatalogWork sCatalogWork : catalogWork.getAnsweringList()) {
                            sCatalogWork.setIsStu(catalogWork1.getIsStu());
                            sCatalogWork.setSynch(catalogWork1.getSynch());
                            catalogWorkService.updateSCatalogWork(sCatalogWork);
                            //查看内容的id
                            list.add(sCatalogWork.getId());
                        }
                        SCatalogWork catalogWork2 = new SCatalogWork();
                        catalogWork2.setCatalogId(catalogWork.getCatalogId());
                        List<SCatalogWork> sCatalogWorks = catalogWorkService.selectSCatalogWorkList(catalogWork2);
                        for (SCatalogWork sCatalogWork : sCatalogWorks) {
                            //如果查看内容里不包括当前内容 修改其状态
                            if (!list.contains(sCatalogWork.getId())) {
                                SCatalogWork preview2 = catalogWorkService.selectSCatalogWorkById(sCatalogWork.getId());
                                preview2.setSynch("0");
                                preview2.setIsStu("0");
                                catalogWorkService.updateSCatalogWork(preview2);
                            }
                        }
                    } else {
                        //没有选择了查看内容 将状态重置为 0 0
                        SCatalogWork work = new SCatalogWork();
                        work.setCatalogId(catalogWork.getCatalogId());
                        List<SCatalogWork> previews = catalogWorkService.selectSCatalogWorkList(work);
                        for (SCatalogWork sCatalogPreview : previews) {
                            sCatalogPreview.setIsStu("0");
                            sCatalogPreview.setSynch("0");
                            catalogWorkService.updateSCatalogWork(sCatalogPreview);
                        }
                    }
                }
                //删除中间表数据
                catalogWorkSynService.deleteSCatalogWorkSynByCatalogId(catalogWork.getCatalogId());
            } else {
                //没有选择了查看内容 将状态重置为 0 0
                SCatalogWork catalogPreview = new SCatalogWork();
                catalogPreview.setCatalogId(catalogWork.getCatalogId());
                List<SCatalogWork> previews = catalogWorkService.selectSCatalogWorkList(catalogPreview);
                for (SCatalogWork sCatalogPreview : previews) {
                    sCatalogPreview.setIsStu("0");
                    sCatalogPreview.setSynch("0");
                    catalogWorkService.updateSCatalogWork(sCatalogPreview);
                }
            }
            //中间表添加数据
            if (catalogWork.getAnsweringList() != null && catalogWork.getAnsweringList().size() > 0) {
                workSyn.setCreateTime(new Date());
                for (SCatalogWork catalogWork2 : catalogWork.getAnsweringList()) {
                    workSyn.setWorkId(catalogWork2.getId());
                    catalogWorkSynService.insertSCatalogWorkSyn(workSyn);
                }
            }
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 根据班级查询其部门信息
     */
    public List<TClass> findDept(List<TClass> classList) {
        if (classList.size() > 0) {
            for (TClass tClass : classList) {
                TSchooldept schooldept = schooldeptService.selectTSchooldeptByClassId(tClass.getDeptId());
                //选择在授班级时上级部门查询错误修改
                if (StringUtils.isNotNull(schooldept)) {
                    tClass.setDeptId(schooldept.getId());
                    tClass.setDeptName(schooldept.getDeptName());
                }
            }
        }
        return classList;
    }

    /**
     * 查看班级  2
     */
    public List<TClass> answerClass2(Integer catalogId) {
        //根据查看内容 查询 查看班级
        List<TClass> classList = classService.selectTClassByCatalogIdWorkAll(catalogId);
        List<TClass> dept = new ArrayList<>();
        if (classList != null && classList.size() > 0) {
            dept = findDept(classList);
            return dept;
        }
        return dept;
    }

    /**
     * 查看内容 针对于左侧目录  1
     */
    public List<SCatalogWork> lookContent(Integer catalogId) {
        List<SCatalogWork> sCatalogWorks = catalogWorkService.selectSCatalogWorkByCatalogId(catalogId);
        return sCatalogWorks;
    }

    /**
     * 查看内容
     */
    @GetMapping("/list")
    public ApiReturnObject list(SCatalogWork catalogWork) {
        catalogWork.setSynch("1");
        List<SCatalogWork> sCatalogWorks = catalogWorkService.selectSCatalogWorkList(catalogWork);
        return new ApiReturnObject("200", "查询成功", sCatalogWorks);
    }


    @GetMapping("/catalogWorklist")
    public ApiReturnObject catalogWorklist(SCatalogWork catalogWork, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SCatalogWork> sCatalogWorks = catalogWorkService.selectSCatalogWorkAllList(catalogWork);
        PageInfo<SCatalogWork> pageInfo = new PageInfo<>(sCatalogWorks);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }


    /**
     * 查询课程问答列表
     */
    @GetMapping("/listAll")
    public ApiReturnObject listAll(Integer catalogId) {
        //  查看班级字符串  查看内容字符串  是否同步  学生查看    名称拼接
        //根据当前id 查询出列表
        if (catalogId != null) {
            SCatalogWork catalogWork = new SCatalogWork();
            catalogWork.setCatalogId(catalogId);

            HashMap<String, Object> map = new HashMap<>();
            //班级字符串
            List<TClass> classList = answerClass2(catalogId);
            map.put("classList", classList);
            if (classList != null && classList.size() > 0) {
                String s = "";
                int size = classList.size();
                //拼接查看班级字符串
                for (int i = 0; i < size; i++) {
                    if (i == size - 1) {
                        //最后一个
                        s = s + classList.get(i).getClassName();
                    } else {
                        s = s + classList.get(i).getClassName() + ",";
                    }
                }
                map.put("classString", s);
            } else {
                map.put("classString", null);
            }
            //查看内容字符串
            List<SCatalogWork> resList = lookContent(catalogId);
            map.put("resList", resList);
            if (resList != null && resList.size() > 0) {
                String s = "";
                int size = resList.size();
                //拼接查看班级字符串
                for (int i = 0; i < size; i++) {
                    if (i == size - 1) {
                        //最后一个
                        s = s + resList.get(i).getWorkText();
                    } else {
                        s = s + resList.get(i).getWorkText() + ",";
                    }
                }
                map.put("resString", s);

                SCatalogWork catalogWork1 = resList.get(0);
                //是否同步
                if (catalogWork1.getSynch() != null) {
                    if ("0".equals(catalogWork1.getSynch())) {
                        map.put("synchBool", false);
                    } else if ("1".equals(catalogWork1.getSynch())) {
                        map.put("synchBool", true);
                    }
                } else {
                    map.put("synchBool", false);
                }

//                    if(catalogId.equals("")){
//                        map.put("catalogId",true);
//                    }else{
//                        map.put("catalogId",false);
//                    }
                //是否允许学生查看
                if (catalogWork1.getIsStu() != null) {
                    if ("0".equals(catalogWork1.getIsStu())) {
                        map.put("stuBool", false);
                    } else if ("1".equals(catalogWork1.getIsStu())) {
                        map.put("stuBool", true);
                    }
                } else {
                    map.put("stuBool", false);
                }
            } else {
                map.put("synchBool", false);
                map.put("stuBool", false);
                map.put("resString", null);
            }

            //资源的分类
            List<SCatalogWork> sCatalogWorks = catalogWorkService.selectSCatalogWorkList(catalogWork);

            //格式化创建时间
            map.put("answeringList", sCatalogWorks);
            return new ApiReturnObject("200", "查询成功", map);
        }
        return new ApiReturnObject("201", "缺少catalogId", "");
    }


    /**
     * 备课模块-课后作业新增-老师端
     *
     * @param catalogWork
     * @return
     * @throws ParseException
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody SCatalogWork catalogWork) throws ParseException {
        if (catalogWork.getCatalogId() != null && catalogWork.getAttendTimeString() != null && catalogWork.getEndTimeString() != null) {
            catalogWork.setSynch("1");
            catalogWork.setIsStu("1");
            catalogWork.setSysState("0");
            catalogWork.setCreateTime(new Date());
            //将字符串日期转换为日期类型
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            catalogWork.setStartTime(format.parse(catalogWork.getAttendTimeString()));
            catalogWork.setEndTime(format.parse(catalogWork.getEndTimeString()));
            catalogWorkService.insertSCatalogWork(catalogWork);
            return new ApiReturnObject("200", "添加成功", "");
        }
        return new ApiReturnObject("201", "缺少catalogId", "");

    }

    /**
     * 获取到最上面一级的id     查看班级列表 课程分享班级
     */
    public Integer GetFirstId2(Integer catalogId) {
        SCatalogMenu sCatalogMenu = catalogMenuService.selectSCatalogMenuById(catalogId);
        //第二级
        if (sCatalogMenu.getParentId() != 0) {
            //第一级
            SCatalogMenu sCatalogMenu1 = catalogMenuService.selectSCatalogMenuById(sCatalogMenu.getParentId());
            return GetFirstId2(sCatalogMenu1.getId());
        }
        return sCatalogMenu.getId();
    }

    /**
     * 修改此门备课的状态
     */
    public SCatalogMenu changeStatus(SCatalogWork work) {
        //将资源所在的备课状态改变
        Integer rCatalogId = work.getCatalogId();
        //教师创建课程表
        SCatalogMenu sCatalog = catalogMenuService.selectSCatalogMenuById(rCatalogId);
        //目录最下面一级
        Integer parentId = sCatalog.getParentId();
        //备课课程id
        Integer integer = GetFirstId2(parentId);
        SCatalogMenu catalogMenu = catalogMenuService.selectSCatalogMenuById(integer);
        return catalogMenu;
    }

    public SCatalogWork updateStatus(SCatalogWork work) {
        //app 端查看 允许
        String i = "1";
        if (i.equals(work.getIsStu())) {
            SCatalogMenu catalogMenu = changeStatus(work);
            if (!i.equals(catalogMenu.getIsStu())) {
                //修改备课是否允许学生查看状态
                catalogMenu.setIsStu("1");
                catalogMenuService.updateSCatalogMenu(catalogMenu);
            }
        }
        // 同步到上课资料
        if (i.equals(work.getSynch())) {
            //同步上课资料
            SCatalogMenu catalogMenu = changeStatus(work);
            if (!i.equals(catalogMenu.getIsSyn())) {
                //修改同步上课资料状态
                catalogMenu.setIsSyn("1");
                catalogMenuService.updateSCatalogMenu(catalogMenu);
            }
        }
        return work;
    }

    /**
     * 修改课后作业
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody SCatalogWork catalogWork) {
//        if(catalogWork.getId() != null){
        //修改某个字段 避免空值报错 针对于查看内容中的课程问答状态
        //修改 查看内容 状态  修改上级
        if (catalogWork.getCatalogId() != null) {
            updateStatus(catalogWork);
        }
        if (catalogWork.getContentList() != null && catalogWork.getContentList().size() > 0) {
            for (SCatalogWork catalogWork1 : catalogWork.getContentList()) {
                if (catalogWork.getIsStu() != null && !"".equals(catalogWork.getIsStu())) {
                    catalogWork1.setIsStu(catalogWork.getIsStu());
                }
                if (catalogWork.getSynch() != null && !"".equals(catalogWork.getSynch())) {
                    catalogWork1.setSynch(catalogWork.getSynch());
                }
                catalogWorkService.updateSCatalogWork(catalogWork1);
            }
            return new ApiReturnObject("200", "修改成功", "");
        } else {
            return new ApiReturnObject("202", "请选择分享内容", "");
        }
//        }
//        return new ApiReturnObject("201","缺少id","");
    }

    /**
     * 修改课程问答
     */
    @PostMapping("/updateData")
    public ApiReturnObject updateData(@RequestBody SCatalogWork catalogWork) {
        if (catalogWork.getId() != null) {
            //修改是否允许学生查看    是否同步上课资料
            catalogWorkService.updateSCatalogWork(catalogWork);
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少id", "");
    }

    /**
     * 删除课后作业
     */
    @PostMapping("/del")
    public ApiReturnObject remove(@RequestBody SCatalogWork catalogWork) {
        if (catalogWork.getId() != null) {
            //查看此内容是否已分享
            SCatalogWorkSyn workSyn = new SCatalogWorkSyn();
            workSyn.setWorkId(catalogWork.getId());
            List<SCatalogWorkSyn> sCatalogWorkSyns = catalogWorkSynService.selectSCatalogWorkSynList(workSyn);
            if (sCatalogWorkSyns != null && sCatalogWorkSyns.size() > 0) {
                return new ApiReturnObject("201", "此内容正在分享", "");
            }
            catalogWorkService.deleteSCatalogWorkById(catalogWork.getId());
            return new ApiReturnObject("200", "删除成功", "");
        }
        return new ApiReturnObject("201", "缺少id", "");
    }


    /**
     * 教师删除作业
     *
     * @param id 作业id
     * @return
     */
    @GetMapping("/deleteSCatalogWorkById")
    public ApiReturnObject remove(Integer id) {
        if (id != null) {
            //作业是否满足删除条件(有人作答禁止删除)
            Integer workAnswerNum = studentWorkService.countWorkAnswerNumByWorkId(id);
            if (workAnswerNum > 0) {
                return new ApiReturnObject("201", "作业已被作答禁止删除", "");
            }
            catalogWorkService.deleteSCatalogWorkById(id);
            return new ApiReturnObject("200", "删除成功", "");
        }
        return new ApiReturnObject("201", "缺少id", "");
    }

}